import type { FormItemRule } from 'element-plus'
import type { Ref } from 'vue'

import { domainRules } from './domain'

export const useRules = (delDisabled: Ref<boolean>) => {
  return computed<Record<string, FormItemRule[]>>(() => ({
    domains: [...domainRules],
    conditions: [
      ...(!delDisabled.value
        ? [
            {
              trigger: 'blur',
              required: true,
              validator(_, value) {
                if (Array.isArray(value)) {
                  const item = value.some(v => v.value)
                  if (!item) {
                    return new Error('请添加条件')
                  }
                }
                return true
              }
            }
          ]
        : [])
    ]
  }))
}
